package cn.nebula.music.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.nebula.music.interceptor.LoginInterceptor;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {

	// 这个方法是用来配置静态资源的，比如html，js，css等
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry register) {
		register.addResourceHandler("/picture/**").addResourceLocations("file:D:\\Program Files\\nebula\\picture\\");
		register.addResourceHandler("/song/**").addResourceLocations("file:D:\\Program Files\\nebula\\song\\");
		register.addResourceHandler("/lyrics/**").addResourceLocations("file:D:\\Program Files\\nebula\\lyrics\\");
		register.addResourceHandler("/mv/**").addResourceLocations("file:D:\\Program Files\\nebula\\mv\\");
	}
	
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
        .allowedOrigins("http://localhost:8080","http://127.0.0.1:8080")
        .allowedMethods("*")
        .allowedHeaders("*")
        // 允许发送Cookie
        .allowCredentials(true)
        // 允许所有方法
        .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD");
	}

	// 这个方法用来注册拦截器，我们自己写好的拦截器需要同个这里添加注册才能生效
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// addPathPattern("/**");//表示拦截所有的请求
		// excludePathPatterns("/login","/register")表示除了登录和注册之外，因为登录注册不需要登录
		
		
		  InterceptorRegistration loginRegistry =
		  registry.addInterceptor(LoginInterceptor()).addPathPatterns("/userInfo").addPathPatterns("/index");
		 
		 
	}

	@Bean
	public LoginInterceptor LoginInterceptor() {
		return new LoginInterceptor();
	}
}
